<template>
  <div class="city_body">
    <div class="city_list">
      <div class="city_hot">
        <h2>定位城市</h2>
        <ul class="clearfix">
          <li class="position">定位失败,请点击重试</li>
        </ul>
      </div>

      <div class="city_hot">
        <h2>热门城市</h2>
        <ul class="clearfix">
          <li
            v-for="item in hotList"
            :key="item.id"
            @click="handleToCity(item.nm, item.id)"
          >{{item.nm}}</li>
        </ul>
      </div>
      <div class="city_sort" ref="city_sort">
        <div v-for="(item,index) in cityList" :key="index">
          <h2>{{item.index}}</h2>
          <ul>
            <li
              v-for="value in item.list"
              :key="value.id"
              @click="handleToCity(value.name, value.id)"
            >{{value.name}}</li>
          </ul>
        </div>
      </div>
      <div class="city_index">
        <ul class="letterList">
          <li
            v-for="(item,index) in cityList"
            :key="index"
            @touchstart="handleToIndex(index)"
          >{{item.index}}</li>
        </ul>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "city",
  data() {
    return {
      //声明两个数组存取要展示的数据
      cityList: [],
      hotList: [],
    };
  },
  methods: {
    handleToCity(nm, id) {
      console.log(111);
      this.$store.commit("city/CITY_INFO", { nm, id });
      window.localStorage.setItem("nowNm", nm);
      window.localStorage.setItem("nowId", id);
      this.$router.push("/movie/nowPlaying");
    },
    handleToIndex(index) {
      var h2 = this.$refs.city_sort.getElementsByTagName("h2");
      this.$refs.city_sort.parentNode.scrollTop = h2[index].offsetTop;
    },
    dataChangeForm(cities) {
      var cityList = [];
      var hotList = [];

      for (var i = 0; i < cities.length; i++) {
        if (
          cities[i].py == "shanghai" ||
          cities[i].py == "beijing" ||
          cities[i].py == "guangzhou" ||
          cities[i].py == "shenzhen" ||
          cities[i].py == "wuhan" ||
          cities[i].py == "tianjin" ||
          cities[i].py == "xian" ||
          cities[i].py == "nanjing" ||
          cities[i].py == "hangzhou" ||
          cities[i].py == "chongqing" ||
          cities[i].py == "chengdu"
        ) {
          hotList.push(cities[i]);
        }
      }

      for (var i = 0; i < cities.length; i++) {
        var firstLetter = cities[i].py.substring(0, 1).toUpperCase();
        if (toCom(firstLetter)) {
          //新添加index
          cityList.push({
            index: firstLetter,
            list: [{ name: cities[i].nm, id: cities[i].id }],
          });
        } else {
          //累加到已有index中
          for (var j = 0; j < cityList.length; j++) {
            if (cityList[j].index === firstLetter) {
              cityList[j].list.push({
                name: cities[i].nm,
                id: cities[i].id,
              });
            }
          }
        }
      }

      cityList.sort((n1, n2) => {
        if (n1.index > n2.index) {
          return 1;
        } else if (n1.index < n2.index) {
          return -1;
        } else {
          return 0;
        }
      });

      function toCom(firstLetter) {
        for (var i = 0; i < cityList.length; i++) {
          if (cityList[i].index === firstLetter) {
            return false;
          }
        }
        return true;
      }

      return {
        cityList,
        hotList,
      };
    },
    /* dataChangeForm(cities) {
      var cityList = [];
      var hotList = [];

      for (var i = 0; i < cities.length; i++) {
        if (cities[i].isHot === 1) {
          hotList.push(cities[i]);
        }
      }

      for (var i = 0; i < cities.length; i++) {
        var firstLetter = cities[i].pinyin.substring(0, 1).toUpperCase();
        if (toCom(firstLetter)) {
          //新添加index
          cityList.push({
            index: firstLetter,
            list: [{ name: cities[i].name, id: cities[i].cityId }],
          });
        } else {
          //累加到已有index中
          for (var j = 0; j < cityList.length; j++) {
            if (cityList[j].index === firstLetter) {
              cityList[j].list.push({
                name: cities[i].name,
                id: cities[i].cityId,
              });
            }
          }
        }
      }

      cityList.sort((n1, n2) => {
        if (n1.index > n2.index) {
          return 1;
        } else if (n1.index < n2.index) {
          return -1;
        } else {
          return 0;
        }
      });

      function toCom(firstLetter) {
        for (var i = 0; i < cityList.length; i++) {
          if (cityList[i].index === firstLetter) {
            return false;
          }
        }
        return true;
      }

      return {
        cityList,
        hotList,
      };
    },*/
  },
  mounted() {
    //   在这里发起数据请求，包括热门城市与城市列表
    /* this.axios.get("/ajax/cinemaDetail?cinemaId=890").then((res) => {
      console.log(res);
    }); */
    /* this.axios({
      url: "https://m.maizuo.com/gateway?k=8950248",
      headers: {
        "X-Client-Info":
          '{"a": "3000","ch": "1002","v": "5.0.4","e": "1599439552343623153483778","bc": "110100"}',
        "X-Host": "mall.film-ticket.city.list",
      },
    }).then((res) => {
      var msg = res.data.msg;
      if (msg === "ok") {
        var cities = res.data.data.cities;
        var { cityList, hotList } = this.dataChangeForm(cities);
        console.log(cityList);
        this.hotList = hotList;
        this.cityList = cityList;
      }
    }); */
    this.axios.get("/api/movie/city").then((res) => {
      console.log(res.status);
      var msg = res.status;
      if (msg === 200) {
        var cities = res.data.data;
        var { cityList, hotList } = this.dataChangeForm(cities);
        this.cityList = cityList;
        this.hotList = hotList;
      }
    });
  },
};
</script>

<style scoped lang="scss">
#content .city_body {
  margin-top: 45px;
  display: flex;
  width: 100%;
  position: absolute;
  top: 0;
  bottom: 0;
}
.city_body .city_list {
  flex: 1;
  overflow: auto;
  background: #f5f5f5;
}
.city_body .city_list::-webkit-scrollbar {
  background-color: transparent;
  width: 0;
}
.city_body .city_hot {
  margin-top: 20px;
  .position {
    width: auto;
    min-width: 30%;
    padding: 0 20px;
  }
}
.city_body .city_hot h2 {
  padding-left: 15px;
  line-height: 30px;
  font-size: 14px;
  background: #f0f0f0;
  font-weight: normal;
}
.city_body .city_list ul li {
  width: 90%;
  height: 44px;
  line-height: 44px;
  display: flex;
  align-items: center;
  border-bottom: 1px solid #c8c7cc;
  font-size: 14px;
  color: #333;
}
.city_body .city_hot ul li {
  float: left;
  background: #fff;
  width: 29%;
  height: 33px;
  display: flex;
  justify-content: center;
  align-items: center;
  margin-top: 15px;
  margin-left: 3%;
  padding: 0 4px;
  border: 1px solid #e6e6e6;
  border-radius: 3px;
  line-height: 33px;
  text-align: center;
  box-sizing: border-box;
}

.city_body .city_sort div {
  margin-top: 20px;
}
.city_body .city_sort h2 {
  padding-left: 15px;
  line-height: 30px;
  font-size: 14px;
  background: #f0f0f0;
  font-weight: normal;
}
.city_body .city_sort ul {
  padding-left: 10px;
  margin-top: 10px;
}
.city_body .city_sort ul li {
  line-height: 30px;
  line-height: 30px;
}
.city_body .city_index {
  width: 20px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  text-align: center;
  border-left: 1px #e6e6e6 solid;
}
.letterList {
  position: fixed;
  top: 120px;
  z-index: 10;
  right: 0;
}
.city_body .city_index .letterList li {
  width: 15px;
  line-height: 25px;
  height: 25px;
  border: none;
}
</style>
